package com.foreveross.proxyip.core.pools.thread;

import com.foreveross.proxyip.core.pools.AvailablePool;
import com.foreveross.proxyip.core.redis.RedisLock;
import com.foreveross.proxyip.core.zk.ZkAppCenter;
import com.foreveross.proxyip.core.zk.ZkPlan;

public abstract class AbsCheckOutThread extends CircleThread{

	public AbsCheckOutThread(AvailablePool pool, CheckType checkType) throws Exception {
		super(pool, checkType);
	}
	
//	/**
//	 * 一次执行到最后的操作
//	 */
	@Override
	public void executeFinish() throws Exception {
//		this.sleep();
	}

	public String getName(){
		return "检出";
	}
	
	@Override
	public void publishTask() throws Exception {
		RedisLock lock = new RedisLock(this.pool.poolSign+"_CheckOutTasklock");//需要加锁，不能并行。
		
		// 只能上锁成功才做，否则放弃此次操作
		if (!lock.tryLock()) {
			return;
		}
		
		try{//
			//验证结果要保存到redis
			log.info(this.pool.poolSign+" checkout");
			
			ZkPlan plan = new ZkPlan(this.pool.poolSign, "CheckOutTask", this.pool.size(),ZkAppCenter.getInstance());
			plan.execute();
			
			log.debug(this.pool.poolSign+" checkOutThread");
		}finally{
			lock.unlock();
		}
	}

	@Override
	public boolean needSleep() throws Exception {
		//这个不需要睡眠
		return false;
	}
}
